<?php
/**
 * Created by PhpStorm.
 * User: mars
 * Date: 2019/2/15
 * Time: 1:21 AM
 */

namespace api\models;

/**
 * Class Complaints
 * @package api\models
 * @property array $images
 */

class Complaints extends \common\models\Complaints
{
    public $images;


    public function fields()
    {

        return [
            'id',
            'uid',
            'status',
            'target_id',
            'type',
            'images'     => function ($model) {
                /**
                 * @var Complaints $model
                 */
                $res = [];
                foreach ($model->complaintsImgs as $complaints_img) {
                    array_push($res,
                        $complaints_img->image->getImageUrl()
                    );
                }

                return $res;
            },

            'content',
            'contact_name',
            'contact_phone',
            'created_at' => function ($model) {
                return \Yii::$app->formatter->asDatetime($model->created_at);
            },

        ];
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
//            [
//                [
//                    'uid',
//                    'type',
//                    'created_at',
//                    'updated_at'
//                ],
//                'integer'
//            ],
            [
                [
                    'contact_name',
                    'contact_phone',
                    'content'
                ],
                'string',
                'max' => 255
            ],
            [['images'], 'safe'],
            [
                ['uid'],
                'exist',
                'skipOnError'     => true,
                'targetClass'     => User::className(),
                'targetAttribute' => ['uid' => 'id']
            ],
        ];
    }


    public function beforeSave($insert)
    {
        $this->uid = \Yii::$app->user->id;
        if ($insert) {
            if ( ! isset($this->status)) {
                $this->status = Complaints::STATUS_WAIT;
            }
            $this->created_at = $this->updated_at = time();
        } else {
            $this->updated_at = time();
        }

        return parent::beforeSave($insert);
    }

    public function afterSave($insert, $changedAttributes)
    {
//        var_dump($this->images);
        if ( ! empty($this->images)) {
            foreach ($this->images as $image) {
                $imageModel               = new ComplaintsImages();
                $imageModel->image_id     = $image;
                $imageModel->complaint_id = $this->id;
                $imageModel->save(false);
            }
        }
        parent::afterSave($insert, $changedAttributes);
    }

    public function afterFind()
    {
        if ( ! isset($this->status)) {
            $this->status = Complaints::STATUS_WAIT;
        }
        parent::afterFind(); // TODO: Change the autogenerated stub
    }


}